import { message } from 'antd';
export default {
  namespace: 'ds',

  state: {
    mainZmcStates: {}, // 上料控制器状态
    camZmcStates: {}, // 分流机控制器状态
    cameraGroupStates: {}, // 相机组控制器状态
    autoState: false, // 后台自动作业是否停止
    mainAlarmInfo: '', // 铡刀总控制器的报警信息框
    mainAlarmInfoVisible: false, // 底层报警提示框是否显示
    grateHidded: false, // 光栅被挡
    putBoardPos: '收料平台1', // 放板位置
    boardThicknessMax: [], // 叠板厚度表
    agvArgsData: [], // AGV参数表
    fullStateLV01: '空闲', //
    fullStateLV02: '空闲', //
    pinFlag: true,
    changeLv1Button: true,
    changeLv2Button: true,
    BMSR: true,
    infoStartAGV: ' ',
  },

  // 和后台交互
  effects: {
    *handleDeviceStatesMessage({ payload }, { call, put }) {
      const { msg } = payload;
      console.log(msg);
      yield put({
        type: 'updateDeviceStates',
        payload: msg,
      });
    },

    *jackingUpStateLV01({ payload }, { call, put }) {
      const response = yield call(
        window.eel.run_py,
        'mainZmc',
        'web_request_jacking_up_state_lv01',
        []
      );
      if (response[0] === 'ok') {
        message.success('清除成功', 3);
        yield put({
          type: 'updateFullState',
          payload: response[1],
        });
      } else {
        message.error('清除失败,原因:' + response, 3);
      }
    },

    *jackingUpStateLV02({ payload }, { call, put }) {
      const response = yield call(
        window.eel.run_py,
        'mainZmc',
        'web_request_jacking_up_state_lv02',
        []
      );
      if (response[0] === 'ok') {
        message.success('清除成功', 3);
        yield put({
          type: 'updateFullState',
          payload: response[1],
        });
      } else {
        message.error('清除失败,原因:' + response, 3);
      }
    },

    *saveFlatMax({ payload }, { call, put }) {
      const response = yield call(
        window.eel.run_py,
        'mainZmc',
        'web_request_save_flat_height_max',
        [payload]
      );
      if (response[0] === 'ok') {
        message.success('保存成功', 3);
        yield put({
          type: 'updateFoldInfoHeight',
          payload: response[1],
        });
      } else {
        message.error('保存失败,原因:' + response, 3);
      }
    },
    *updateAgvArgs({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'db', 'web_request_update_agv_args', [
        payload,
      ]);
      console.log('response', response);
      if (response[0] === 'ok') {
        message.success('保存成功', 3);
        yield put({
          type: 'updateAgvArgsData',
          payload: response[1],
        });
      } else {
        message.error('保存失败,原因:' + response[1], 3);
      }
    },
    *updateBoardThickness({ payload }, { call, put }) {
      const response = yield call(
        window.eel.run_py,
        'mainZmc',
        'web_request_update_board_thickness',
        [payload]
      );
      if (response[0] === 'ok') {
        message.success('保存成功', 3);
        yield put({
          type: 'updateBoardThickNess',
          payload: response[1],
        });
      } else {
        message.error('保存失败,原因:' + response, 3);
      }
    },

    *changePutBoardPos({ payload }, { call, put }) {
      const response = yield call(
        window.eel.run_py,
        'mainZmc',
        'web_request_change_put_board_pos',
        []
      );
      if (response[0] == 'ok') {
        message.success('切换成功!', 3);
        yield put({
          type: 'updatePutBoardPos',
          payload: response[1],
        });
      } else {
        message.error('切换失败,' + response[1], 5);
      }
    },
    *ExcepReset({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'mainZmc', 'web_request_excep_reset', []);
    },
  },

  // 状态更新
  reducers: {
    updateFullState(state, action) {
      return {
        ...state,
        fullStateLV01: action.payload[0],
        fullStateLV02: action.payload[1],
      };
    },
    updateDeviceStates(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
    updateCameraStates(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
    closeMainAlarmInfoModal(state, action) {
      return {
        ...state,
        mainAlarmInfoVisible: false,
        mainAlarmInfo: '',
      };
    },
    updatePutBoardPos(state, action) {
      return {
        ...state,
        putBoardPos: action.payload,
      };
    },
    updateBoardThickNess(state, action) {
      return {
        ...state,
        boardThicknessMax: action.payload,
      };
    },
    closeMainRasterModal(state, action) {
      return {
        ...state,
        rasterAlarmVisible: false,
      };
    },
    updateAgvArgsData(state, action) {
      return {
        ...state,
        agvArgsData: action.payload,
      };
    },
  },
};
